package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ujiuye.mapper.*;
import com.ujiuye.pojo.Checkgroup;
import com.ujiuye.pojo.CheckgroupCheckitem;
import com.ujiuye.pojo.Checkitem;
import com.ujiuye.pojo.SetmealCheckgroup;
import com.ujiuye.service.IInfoService;
import com.ujiuye.vo.InfoVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class InfoServiceImpl implements IInfoService {

    @Resource
    private InfoMapper infoMapper;
    @Resource
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Resource
    private CheckitemMapper checkitemMapper;

    @Override
    public InfoVo listInfoVo(int memberId) {
        InfoVo infoVo = infoMapper.listInfoVo(memberId);
        List<Checkgroup> checkgroupList = new ArrayList<>();//用于保存checkgroup
        Integer setmealId = infoVo.getSetmealId();
        QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
        setmealCheckgroupQueryWrapper.eq("setmeal_id",setmealId);
        List<SetmealCheckgroup> setmealCheckgroupList = setmealCheckgroupMapper.selectList(setmealCheckgroupQueryWrapper);
        if (setmealCheckgroupList!=null && setmealCheckgroupList.size()>0){
            for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroupList) {
                Checkgroup checkgroup = checkgroupMapper.selectById(setmealCheckgroup.getCheckgroupId());
                QueryWrapper<CheckgroupCheckitem> checkgroupCheckitemQueryWrapper = new QueryWrapper<>();
                checkgroupCheckitemQueryWrapper.eq("checkgroup_id",checkgroup.getId());
                List<CheckgroupCheckitem> checkgroupCheckitemList = checkgroupCheckitemMapper.selectList(checkgroupCheckitemQueryWrapper);
                List<Checkitem> checkitemList = new ArrayList<>();//用于保存checkitem
                if (checkgroupCheckitemList!=null && checkgroupCheckitemList.size()>0){
                    for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitemList) {
                        Checkitem checkitem = checkitemMapper.selectById(checkgroupCheckitem.getCheckitemId());
                        checkitemList.add(checkitem);
                    }
                    checkgroup.setCheckitemList(checkitemList);
                }
                checkgroupList.add(checkgroup);
            }
            infoVo.setCheckgroupList(checkgroupList);
        }
        return infoVo;
    }


}
